import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/views/Login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/',
      redirect: '/dashboard',
      component: () => import('@/layouts/AdminLayout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: 'dashboard',
          name: 'Dashboard',
          component: () => import('@/views/Dashboard.vue'),
          meta: { title: '仪表盘' }
        },
        // 用户管理
        {
          path: '/user/list',
          name: 'UserList',
          component: () => import('@/views/user/UserList.vue'),
          meta: { title: '用户列表' }
        },
        {
          path: '/user/statistics',
          name: 'UserStatistics',
          component: () => import('@/views/user/UserStatistics.vue'),
          meta: { title: '用户统计' }
        },
        // 设备管理
        {
          path: '/device/list',
          name: 'DeviceList',
          component: () => import('@/views/device/DeviceList.vue'),
          meta: { title: '设备列表' }
        },
        {
          path: '/device/inventory',
          name: 'DeviceInventory',
          component: () => import('@/views/device/InventoryDevice.vue'),
          meta: { title: '库存设备' }
        },
        {
          path: '/device/map',
          name: 'DeviceMap',
          component: () => import('@/views/device/DeviceMap.vue'),
          meta: { title: '设备地图' }
        },
        {
          path: '/device/statistics',
          name: 'DeviceStatistics',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '设备统计' }
        },
        // 投放管理
        {
          path: '/disposal/records',
          name: 'DisposalRecords',
          component: () => import('@/views/disposal/DisposalRecords.vue'),
          meta: { title: '投放记录' }
        },
        {
          path: '/disposal/pricing',
          name: 'PriceManagement',
          component: () => import('@/views/finance/PriceManagement.vue'),
          meta: { title: '价格管理' }
        },
        {
          path: '/disposal/exceptions',
          name: 'DisposalExceptions',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '异常处理' }
        },
        {
          path: '/disposal/statistics',
          name: 'DisposalStatistics',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '投放统计' }
        },
        // 质检管理
        {
          path: '/quality/exceptions',
          name: 'QualityExceptions',
          component: () => import('@/views/quality/QualityExceptions.vue'),
          meta: { title: '异常投递列表' }
        },

        {
          path: '/quality/records',
          name: 'QualityRecords',
          component: () => import('@/views/quality/QualityRecords.vue'),
          meta: { title: '质检记录' }
        },
        {
          path: '/quality/statistics',
          name: 'QualityStatistics',
          component: () => import('@/views/quality/QualityStatistics.vue'),
          meta: { title: '质检统计' }
        },
        // 清运管理
        {
          path: '/cleanup/tasks',
          name: 'CleanupTasks',
          component: () => import('@/views/cleanup/CleanupTasks.vue'),
          meta: { title: '清运任务' }
        },

        {
          path: '/cleanup/staff',
          name: 'CleanupStaff',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '清运员管理' }
        },
        {
          path: '/cleanup/assignment',
          name: 'CleanupAssignment',
          component: () => import('@/views/cleanup/CleanupAssignment.vue'),
          meta: { title: '清运分配' }
        },
        {
          path: '/cleanup/statistics',
          name: 'CleanupStatistics',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '清运统计' }
        },
        // 财务管理
        {
          path: '/finance/revenue',
          name: 'FinanceRevenue',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '收益统计' }
        },
        {
          path: '/finance/withdrawal',
          name: 'FinanceWithdrawal',
          component: () => import('@/views/finance/WithdrawalManagement.vue'),
          meta: { title: '提现管理' }
        },
        {
          path: '/finance/reports',
          name: 'FinanceReports',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '财务报表' }
        },
        {
          path: '/finance/transactions',
          name: 'FinanceTransactions',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '资金流水' }
        },
        // 区域管理
        {
          path: '/area/list',
          name: 'AreaList',
          component: () => import('@/views/area/AreaList.vue'),
          meta: { title: '区域列表' }
        },
        {
          path: '/area/tree',
          name: 'AreaTree',
          component: () => import('@/views/area/AreaTree.vue'),
          meta: { title: '区域树形' }
        },
        // 内容管理
        {
          path: '/content/banners',
          name: 'ContentBanners',
          component: () => import('@/views/content/ContentBanners.vue'),
          meta: { title: '轮播图管理' }
        },
        {
          path: '/content/banner-groups',
          name: 'ContentBannerGroups',
          component: () => import('@/views/content/ContentBannerGroups.vue'),
          meta: { title: '轮播图组管理' }
        },
        {
          path: '/content/news',
          name: 'ContentNews',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '新闻管理' }
        },
        {
          path: '/content/announcements',
          name: 'ContentAnnouncements',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '公告管理' }
        },
        {
          path: '/content/help',
          name: 'ContentHelp',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '帮助文档' }
        },
        // 统计分析
        {
          path: '/analytics/reports',
          name: 'AnalyticsReports',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '数据报表' }
        },
        {
          path: '/analytics/users',
          name: 'AnalyticsUsers',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '用户分析' }
        },
        {
          path: '/analytics/devices',
          name: 'AnalyticsDevices',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '设备分析' }
        },
        {
          path: '/analytics/trends',
          name: 'AnalyticsTrends',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '趋势分析' }
        },
        // 系统管理
        {
          path: '/system/admins',
          name: 'SystemAdmins',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '管理员管理' }
        },
        {
          path: '/system/roles',
          name: 'SystemRoles',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '角色权限' }
        },
        {
          path: '/system/settings',
          name: 'SystemSettings',
          component: () => import('@/views/system/SystemSettings.vue'),
          meta: { title: '系统设置' }
        },
        {
          path: '/system/logs',
          name: 'SystemLogs',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '操作日志' }
        },
        {
          path: '/system/monitor',
          name: 'SystemMonitor',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '系统监控' }
        },
        // 个人中心
        {
          path: '/profile/info',
          name: 'ProfileInfo',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '个人信息' }
        },
        {
          path: '/profile/password',
          name: 'ProfilePassword',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '修改密码' }
        },
        {
          path: '/profile/logs',
          name: 'ProfileLogs',
          component: () => import('@/views/common/CommonPage.vue'),
          meta: { title: '登录日志' }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      redirect: '/dashboard'
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  // 开发环境下跳过登录验证
  if (import.meta.env.DEV) {
    // 如果用户未登录，自动模拟登录
    if (!userStore.isLoggedIn) {
      userStore.token = 'dev-token-' + Date.now()
      userStore.userInfo = {
        id: 1,
        username: 'admin',
        name: '管理员',
        role: 'admin',
        permissions: ['*'],
        avatar: '',
        phone: '13800138000',
        email: 'admin@example.com',
        createdAt: '2024-01-01 00:00:00'
      }
      userStore.permissions = ['*']
    }
    next()
    return
  }

  // 生产环境下的正常登录验证
  if (to.meta.requiresAuth !== false && !userStore.isLoggedIn) {
    next('/login')
  } else if (to.path === '/login' && userStore.isLoggedIn) {
    next('/dashboard')
  } else {
    next()
  }
})

export default router
